package daosimpl;

import java.util.ArrayList;
import java.util.List;

import model.Curso;
import model.PeriodoAcademico;
import model.Pregunta;
import model.PreguntaEstado;
import model.Profesor;
import model.Respuesta;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import util.HibernateUtil;
import daos.PreguntaDAO;

public class PreguntaDAOImplH implements PreguntaDAO {
	private Transaction tx;

	@Override
	public Pregunta FindByID(String id) throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Pregunta insertPregunta(Pregunta p)  throws HibernateException{
		
		try {
			
			tx = HibernateUtil.session.beginTransaction();
			HibernateUtil.session.save(p);
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}
			return p;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return null;        	
	}

	@Override
	public List<Pregunta> ListarPreguntas() throws HibernateException {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

	@Override
	public List<Pregunta> ListarPreguntasxCurso(Curso curso) throws HibernateException {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta where curso_id = '"+curso.getCursoId()+"' AND estado_id=0001");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}
	
	@Override
	public Pregunta updateByRespuesta(String id, Respuesta correcta)
			throws HibernateException {
		      try{
		    	 tx = HibernateUtil.session.beginTransaction();
		    	 Pregunta pregunta = 
		                    (Pregunta) HibernateUtil.session.get(Pregunta.class, id); 
		    	 pregunta.setRespuesta(correcta);
		    	 HibernateUtil.session.update(pregunta); 
		         tx.commit();
		         return pregunta;
		      }catch (HibernateException e) {
		         if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
		      }finally {
		         
		      }
		      return null;
		   
	}

	@Override
	public List<Pregunta> ListarPreguntasxPeriodo(PeriodoAcademico per,Curso c)
			throws HibernateException {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta where fecha_aceptacion BETWEEN '"+per.getFechaInicio()+"'	AND '"+per.getFechaFin()+"' AND curso_id = '"+c.getCursoId()+"'"
							+ "AND estado_id=0001");
			result = query.list();
			System.out.println(per.getFechaInicio());
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}
	
	@Override
	public List<Pregunta> ListarPreguntasxEstadoxCurso(Curso curso,String estado) throws HibernateException {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta where curso_id = '"+curso.getCursoId()+"' and preguntaEstado.estadoId='"+estado+"'");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}
	
	public void updatePregunta(Pregunta p)throws HibernateException{
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			 HibernateUtil.session
				.update(p);
			tx.commit();
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
	        //session.close(); 
	    }  
		
	}

	@Override
	public List<Pregunta> ListarPreguntasxProfesor(Profesor p)
			throws HibernateException {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {
			System.out.println(p.getDocenteId()+" fjfjfjf");
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta where propuso_docente_id= '"+p.getDocenteId()+"' AND estado_id=0001");
			result = query.list();
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}
	
	public int cantPregxProfesor(String codProf) {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {

			tx = HibernateUtil.session.beginTransaction();

			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta where profesorByPropusoDocenteId.docenteId = '"
							+ codProf + "'");					
			if (query.list().isEmpty()) {
				System.out.println("esta vacio T");
			}
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			tx.commit();
			if (!tx.wasCommitted()) {
				tx.commit();
			}
			return result.size();

		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback();
			return 0;
		} finally {
			// session.close();			
		}
	}

	public int cantPregxProfesoxEstado(String codProf, String estado) {
		List<Pregunta> result = new ArrayList<Pregunta>();
		try {

			tx = HibernateUtil.session.beginTransaction();

			Query query = HibernateUtil.session
					.createQuery("FROM Pregunta where profesorByPropusoDocenteId.docenteId = '"
							+ codProf+ "'"
							+ "and preguntaEstado.estadoId='"
							+ estado + "'");						
			if (query.list().isEmpty()) {
				System.out.println("esta vacio P");
			}
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			tx.commit();
			if (!tx.wasCommitted()) {
				tx.commit();
			}
			return result.size();

		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback();
			return 0;
		} finally {
			// session.close();
		}
	}

}
